﻿using System;
using System.Collections.Generic;
using System.Drawing;
using System.Drawing.Imaging;
using System.Drawing.Text;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Point = Microsoft.Xna.Framework.Point;
using Rectangle = Microsoft.Xna.Framework.Rectangle;

namespace Common
{
    /// <summary>
    /// 动态生成文字图片精灵
    /// </summary>
    public class ImageTextSprite : UserControlledSprite
    {
        public string Text = string.Empty;
        public Font Font=new System.Drawing.Font("方正北魏楷书繁体", 15f);
        public Microsoft.Xna.Framework.Color Color = Microsoft.Xna.Framework.Color.White;
        public Graphics Graphics = Graphics.FromHwnd(IntPtr.Zero);
        private Brush Brush = Brushes.White;
        public ImageTextSprite(string text,Microsoft.Xna.Framework.Color color,Font font,Rectangle drawPosition)
        {
            this.Text = text;
            this.Color = color;
            this.Font = font;
            this.DrawPosition = drawPosition;
            this.Image=CreateTextTexture(text);
        }
        public ImageTextSprite(string text, Microsoft.Xna.Framework.Color color, Rectangle drawPosition)
        {
            this.Text = text;
            this.Color = color;
            this.DrawPosition = drawPosition;
            this.Image = CreateTextTexture(text);
        }
        public ImageTextSprite(string text, Rectangle drawPosition)
        {
            this.Text = text;
            this.DrawPosition = drawPosition;
            this.Image = CreateTextTexture(text);
        }
        public ImageTextSprite(string text)
        {
            this.Text = text;
            this.Image = CreateTextTexture(text);
        }
        public ImageTextSprite()
        {
        }
        public override void Draw(GameTime gameTime)
        {
            if (this.Image != null)
            {
                SpriteBatch.Draw(this.Image, this.DrawPosition, null, this.Color);
            }
        }

      
        public Texture2D CreateTextTexture(string text)
        {
            if (((text == null) || (this.Font == null)) || (text.Length == 0))
            {
                return null;
            }
            try
            {
                Size size = Graphics.MeasureString(text, this.Font).ToSize();
                using (Bitmap image = new Bitmap(size.Width, size.Height, PixelFormat.Format32bppArgb))
                {
                    Graphics = Graphics.FromImage(image);
                    Graphics.TextRenderingHint = TextRenderingHint.SystemDefault;
                    Graphics.DrawString(text, this.Font, this.Brush, new PointF(0f, 0f));
                    BItmapToTexture2D3(image);
                }
            }
            catch
            {

            }
            return Image;
        }

        private Texture2D BItmapToTexture2D1(Bitmap b)
        {
            Game.GraphicsDevice.Textures[0] = null;
            Image = new Texture2D(Game.GraphicsDevice, b.Width, b.Height);
            BitmapData bData = b.LockBits(new System.Drawing.Rectangle(new System.Drawing.Point(), b.Size),
                ImageLockMode.ReadOnly,
                PixelFormat.Format32bppRgb);

            // number of bytes in the bitmap  
            int byteCount = bData.Stride * b.Height;
            byte[] bmpBytes = new byte[byteCount];
            // Copy the locked bytes from memory  
            Marshal.Copy(bData.Scan0, bmpBytes, 0, byteCount);

            // don't forget to unlock the bitmap!!  
            b.UnlockBits(bData);

            this.Image.SetData(bmpBytes);

            return Image;
        }
        public Texture2D BItmapToTexture2D2(Bitmap image)
        {
            // Buffer size is size of color array multiplied by 4 because
            // each pixel has four color bytes
            int bufferSize = image.Height * image.Width * 4;

            // Create new memory stream and save image to stream so
            // we don't have to save and read file 
            using (System.IO.MemoryStream memoryStream = new System.IO.MemoryStream(bufferSize))
            {
                image.Save(memoryStream, System.Drawing.Imaging.ImageFormat.Png);
                // Creates a texture from IO.Stream - our memory stream
               this.Image = Texture2D.FromStream(
                Game.GraphicsDevice, memoryStream);
                return this.Image;
            }
        }
        private Texture2D BItmapToTexture2D3(Bitmap image)
        {
            using (MemoryStream stream = new MemoryStream())
            {
                try
                {
                    image.Save(stream, ImageFormat.Png);
                    //image.Save(stream, ImageFormat.Bmp);//这种格式，XNA4.0不支持了，会报错运行。
                    stream.Position = 0;
                    this.Image= Texture2D.FromStream(Game.GraphicsDevice, stream);
                }
                catch (Exception)
                {
                }
            }
            return Image;
        }
    }

}
